﻿using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;
using Hxsoft.AppV1.WebUI;

namespace Hxsoft.AppV1._Controls
{
	[ValidationProperty("Value")]
	public partial class ResourcePopupSelector : Hxsoft.AppV1.UserControl
	{
		public string UploadText
		{
			get { return Res.ResourcePopupSelector_UploadText; }
		}

		public string OtherUrlText
		{
			get { return Res.ResourcePopupSelector_OtherUrlText;}
		}

		public string AddText
		{
			get { return Res.AddText; }
		}

		public string CancelText
		{
			get { return Res.ResourcePopupSelector_CancelText; }
		}

		public string InfoResourceThumbnailGeneratorUrl_ClientScript
		{
			get { return GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl_ClientScript(); }
		}

		public string PreviewCopyName
		{
			get { return GenericCommon.InfoResourceCopyName_ResourceSelector; }
		}



		public Guid GlobalId
		{
			get { return CtrResourceSelector.GlobalId; }
			set { CtrResourceSelector.GlobalId = value; }
		}

		public string InfoType
		{
			get { return CtrResourceSelector.InfoType; }
			set { CtrResourceSelector.InfoType = value; }
		}

		public string FilterResourceTypeName
		{
			get { return CtrResourceSelector.FilterResourceTypeName; }
			set { CtrResourceSelector.FilterResourceTypeName = value; }
		}

		public string Value
		{
			get
			{
				return CtrSelectorValue.Value;
			}
			set
			{
				CtrSelectorValue.Value = value;
			}
		}

		private LoadedValue<int?> loadedPopupWidth;
		public int? PopupWidth
		{
			get
			{
				if (loadedPopupWidth.Loader == null)
					loadedPopupWidth.Loader = () => (int?)ViewState["PopupWidth"];
				return loadedPopupWidth.Value;
			}
			set
			{
				if (loadedPopupWidth.Setter == null)
					loadedPopupWidth.Setter = v => ViewState["PopupWidth"] = v;
				loadedPopupWidth.Value = value;
			}
		}

		private LoadedValue<int?> loadedPopupHeight;
		public int? PopupHeight
		{
			get
			{
				if (loadedPopupHeight.Loader == null)
					loadedPopupHeight.Loader = () => (int?)ViewState["PopupHeight"];
				return loadedPopupHeight.Value;
			}
			set
			{
				if (loadedPopupHeight.Setter == null)
					loadedPopupHeight.Setter = v => ViewState["PopupHeight"] = v;
				loadedPopupHeight.Value = value;
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.JQueryUrl, IncludeFileType.JS);
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.CommonScriptUrl, IncludeFileType.JS);
			this.ApplicationPage.RegisterHeadIncludeFile(ApplicationConfiguration.ResourcePopupSelectorStyleUrl, IncludeFileType.CSS);
			
			CtrResourceSelector.ClientCallbackFunction = this.ClientID + "_SetValue";

			ScriptManager.RegisterStartupScript(this, this.GetType(), this.ID + "_Init", "$(" + this.ClientID + "_Init);", true);

			base.OnPreRender(e);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			if (!string.IsNullOrEmpty(Value))
				CtrImagePreview.ImageUrl = GenericUrlManager.GetInfoResourceThumbnailGeneratorUrl(Value, PreviewCopyName);
			else
				CtrImagePreview.ImageUrl = ApplicationConfiguration.ResourcePopupSelectorFolderUrl + "NotImage.jpg";

			base.Render(writer);
		}
	}
}